package com.xintonghua.bussiness.ui.fragment.cube;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.xintonghua.base.utils.DividerGridItemDecoration;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.StockAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CubeBean;
import com.xintonghua.bussiness.bean.ReserveBean;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作者：wanxianyi
 * 时间：2018/4/25.
 * 包名：com.xintonghua.bussiness.ui.fragment.cube
 */

public class RepertoryActivity extends BaseActivity {
    @BindView(R.id.lv_produce)
    RecyclerView lvProduce;
    @BindView(R.id.lv_sales)
    RecyclerView lvSales;
    Unbinder unbinder;
    StockAdapter adapter, stockAdapter;
    @BindView(R.id.tv_lable_1)
    TextView tvLable1;
    @BindView(R.id.tv_lable_2)
    TextView tvLable2;
    @BindView(R.id.tv_all_atock)
    TextView tvAllAtock;
    @BindView(R.id.tv_lable_3)
    TextView tvLable3;
    @BindView(R.id.tv_kucunjine)
    TextView tvKucunjine;
    @BindView(R.id.tv_jinghuoshu)
    TextView tvJinghuoshu;
    @BindView(R.id.tv_zhichu)
    TextView tvZhichu;
    @BindView(R.id.tv_xiaohuo)
    TextView tvXiaohuo;
    @BindView(R.id.tv_xiaohuo2)
    TextView tvXiaohuo2;
    @BindView(R.id.tv_select_times)
    TextView tv_select_times;
    @BindView(R.id.svScrollview)
    ScrollView svScrollview;
    private List<CubeBean> cubeBeanList, cubeBeanList1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repertory);
        onSimpleActionBar();
        setSimpleActionBar("时间筛选");
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        cubeBeanList = new ArrayList<>();
        cubeBeanList1 = new ArrayList<>();
        getNetData();
    }

    public void getNetData() {
        httpCent.getReserve(getIntent().getIntExtra(BuildConfig.FLAVOR, 0), starttime, endtime, this, MyState.requestOne);
    }

    public void initLianggeList() {
        adapter = new StockAdapter(this, cubeBeanList);
        adapter.setType(2);
        lvProduce.addItemDecoration(new DividerGridItemDecoration(this));
        lvProduce.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        lvProduce.setAdapter(adapter);
        stockAdapter = new StockAdapter(this, cubeBeanList1);
        stockAdapter.setType(1);
        lvSales.addItemDecoration(new DividerGridItemDecoration(this));
        lvSales.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        lvSales.setAdapter(stockAdapter);

    }


    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    bean = JsonUtil.getEntityByJsonString(data.toString(), ReserveBean.class);
                    initData();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.fl_select_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_select_time:
                JumpUtils.jumpActivityForResult(RepertoryActivity.this, DialogStartAndEndTimeActivity.class, null, MyState.requestOne);
                break;
        }
    }

    ReserveBean bean;

    public void initData() {
        tvAllAtock.setText("" + bean.getTotalCount());
        tvKucunjine.setText("" + bean.getTotalPrice());

        tvJinghuoshu.setText("进货+（" + bean.getTotalJinduoCount() + "瓶)");
        tvZhichu.setText("-" + bean.getTotalMoney() + "元");
        tvXiaohuo.setText("销货+（" + bean.getXlCount() + "瓶)");
        tvXiaohuo2.setText("+" + bean.getXlMoney() + "元");
        List<ReserveBean.JinhuoListBean> jing_list = bean.getJinhuoList();
        List<ReserveBean.XiaoliangListBean> xiao_list = bean.getXiaoliangList();

        cubeBeanList = new ArrayList<>();
        cubeBeanList1 = new ArrayList<>();
        for (int i = 0; i < jing_list.size(); i++) {
            cubeBeanList.add(new CubeBean(R.drawable.cube_product, "", jing_list.get(i).getGoods_name(), jing_list.get(i).getTotalPrice(), jing_list.get(i).getCount(), false));
        }
        for (int i = 0; i < xiao_list.size(); i++) {
            cubeBeanList1.add(new CubeBean(R.drawable.cube_product, "", xiao_list.get(i).getName(), xiao_list.get(i).getTotalPrice(), xiao_list.get(i).getCount(), false));
        }

        initLianggeList();
    }

    String starttime = "";
    String endtime = "";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MyState.requestOne && resultCode == Activity.RESULT_OK) {

            if (data != null) {
                starttime = data.getStringExtra("starttime");
                endtime = data.getStringExtra("endtime");

                tv_select_times.setText(starttime + " 至 " + endtime);
                svScrollview.setVisibility(View.VISIBLE);
                getNetData();
            }
        }
    }

}
